<template>
  <van-icon
    :color="likeCount == 1 ? '#f7a306' : '#777'"
    :name="likeCount == 1 ? 'good-job' : 'good-job-o'"
    @click="onLike"
  />
</template>

<script>
import { deleteLike, addLike } from '@/api/article'

export default {
  name: 'LikeArticle',
  props: {
    likeCount: {
      type: Number,
      required: true
    },
    id: {
      type: [Number, String],
      required: true
    }
  },
  methods: {
    async onLike () {
      try {
        this.$toast.loading({
          duration: 0, // 一直展示
          message: '加载中...',
          forbidClick: true // 禁用背景点击
        })

        if (this.likeCount) {
          await deleteLike(this.id)
        } else {
          await addLike(this.id)
        }
        this.$emit('update:like-count', this.likeCount ? 0 : 1)
      } finally {
        this.$toast.clear()
      }
    }
  }
}
</script>

<style>
</style>
